<?php
/**
 * Created by PhpStorm.
 * 司机提现
 */

namespace App\Http\Controllers\Drivers\V2\Drivers;

use App\Http\Controllers\Controller;
use App\Http\Requests\WithdrawRequests;
use App\Services\WithdrawService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class WithdrawController extends Controller
{
    /**
     * 司机申请提现
     */
    public function withdraw_after(Request $request, WithdrawService $service, WithdrawRequests $requests)
    {
        $data = $request->post();
        $type = (string)$request->post('type','0');  // 0 网约车 1 代驾  more:网约车
        if (!$requests->scene('modification')->check($data)) {
            return $this->sendError($requests->getError());
        }
        if ($service->withdraw_add($data, $type,Auth::user())) {
            return $this->sendSuccess('申请成功');
        }
        return $this->sendError('申请失败');
    }

    /**
     * @param Request $request
     * @param WithdrawService $service
     * 账号列表
     */
    public function driver_wallet_list(Request $request, WithdrawService $service)
    {
        $res = $service->driver_wallet_list(Auth::user());
        return $this->sendJson($res);
    }

    /**
     * @param Request $request
     * @param WithdrawService $service
     * 添加账号
     */
    public function driver_wallet_add(Request $request, WithdrawService $service, WithdrawRequests $requests)
    {
        $data = $request->post();
        if (!$requests->scene('add')->check($data)) {
            return $this->sendError($requests->getError());
        }
        $account = (string)$request->post('account', '');
        $type = (string)$request->post('type', '');
        $bank_name = (string)$request->post('bank_name', '');
        $account_name = (string)$request->post('account_name', '');
        $phone = (string)$request->post('phone', '');
        $info = $service->driver_wallet_insert($account, $type, $bank_name, $account_name, $phone, Auth::user());
        if ($info) {
            return $this->sendJson($info);
        }
        return $this->sendError('error');
    }

    /**
     * @param Request $request
     * @param WithdrawService $service
     * 提现详情
     */
    public function driver_wallet_details(Request $request, WithdrawService $service)
    {
        $id = (int)$request->post('id', '');
        if (empty($id)) {
            return $this->sendError('ID不能为空');
        }
        return $this->sendJson($service->driver_withdraw_show($id));
    }

    /**
     * 提现账号删除
     */
    public function driver_wallet_del(Request $request, WithdrawService $service)
    {
        $id = (int)$request->post('id', '');
        if (empty($id))
            return $this->sendError('账户ID不能为空');
        if ($service->driver_wallet_del($id)) {
            return $this->sendSuccess('Ok');
        }
        return $this->sendError('error');
    }

    /**
     * @param Request $request
     * @param WithdrawService
     * $service上一次提现账号
     */
    public function driver_wallet_status(Request $request, WithdrawService $service)
    {
        return $this->sendJson($service->driver_wallet_status(Auth::user()));
    }
}